﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;
using System.Web.Security;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;


namespace TractRecord.Code
{
    public class DataAccess
    {
        public static Database Data
        {
            get; 
            set;
        }
                
        static DataAccess()
        {
            Data = EnterpriseLibraryContainer.Current.GetInstance<Database>("TractRecordDB");
        }

        public static string ProjectName(string ProjectId)
        {
            try
            {

                string userid = Membership.GetUser().ProviderUserKey.ToString();

                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectSummaryForUser_Get");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", userid));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProjectId", ProjectId));

                IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

                if (r.Read())
                {
                    return r["Name"].ToString();
                }
            }
            catch (Exception e)
            {
                Common.Log(e.ToString(), e.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Error, "DataAccess_Error");
            }

            return string.Empty;
        }

        public static Dictionary<string, Hashtable> ProjectEmailList()
        {
            Dictionary<string, Hashtable> retval = new Dictionary<string, Hashtable>();
            try
            {
                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectListByEmail_Get");

                IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

                while (r.Read())
                {
                    string key = r["ProjectEmail"].ToString();

                    Hashtable ht = new Hashtable();

                    ht.Add("ActiveInd", Convert.ToBoolean(r["ActiveInd"]));
                    ht.Add("Name", r["Name"].ToString());
                    ht.Add("ProjectId", (Guid)r["ProjectId"]);

                    retval.Add(key, ht);
                }
            }
            catch (Exception e)
            {
                Common.Log(e.ToString(), e.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Error, "DataAccess_Error");
            }
            return retval;
        }

        public static Guid AddEmail(string EmailAddress, string Character, string Description, DateTime EntryDt, string Subject, string Body)
        {
            try
            {

                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectEmail_Ins");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EmailAddress", EmailAddress));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Character", Character));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Description", Description));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EntryDt", EntryDt));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Subject", Subject));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Body", Body));

                return (Guid)Code.DataAccess.Data.ExecuteScalar(cmd);
            }
            catch (Exception e)
            {
                Common.Log(e.ToString(), e.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Error, "DataAccess_Error");
            }
            
            return Guid.Empty;
        }
        
        public static void AddFile(DateTime EntryDt, Guid ProjectId, string FileLocation, Guid EmailId, string FileName, string MimeType, byte[] FileData)
        {
            try
            {

                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectEmailFile_Ins");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EntryDt", EntryDt));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProjectId", ProjectId));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileLocation", FileLocation));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EmailId", EmailId));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", FileName));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@MimeType", MimeType));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileData", FileData));

                Code.DataAccess.Data.ExecuteNonQuery(cmd);
            }
            catch (Exception e)
            {
                Common.Log(e.ToString(), e.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Error, "DataAccess_Error");
            }
        }
        
        public static ArrayList GetAlertEmails(string ProjectId)
        {
            ArrayList values = new ArrayList();

            try
            {
                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectAlertEmails_Get");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ProjectId", ProjectId));

                IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

                while (r.Read())
                {
                    values.Add(r["Email"].ToString());
                }
            }
            catch (Exception ex)
            {
                Common.Log(ex.ToString(), ex.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Critical, "DataAccess_Error");
                throw;
            }

            return values;
        }


        public static void AutoPurge(int Archive, int Delete, int Purge)
        {
            try
            {

                DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_CleanFiles_Upd");
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Archive", Archive));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Delete", Delete));
                cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Purge", Purge));

                Code.DataAccess.Data.ExecuteNonQuery(cmd);
            }
            catch (Exception e)
            {
                Common.Log(e.ToString(), e.GetType().ToString(), 100, 0, System.Diagnostics.TraceEventType.Error, "DataAccess_Error");
            }
        }
    }
}